package com.easyboot.web.system.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easyboot.framework.domain.DataTableDto;
import com.easyboot.utils.CommonUtil;
import com.easyboot.web.system.domain.SysLog;
import com.easyboot.web.system.domain.SysLoginLog;
import com.easyboot.web.system.mapper.SysLoginLogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 *
 */
@Service
public class SysLoginLogService extends ServiceImpl<SysLoginLogMapper, SysLoginLog>{


    @Autowired
    private SysLoginLogMapper loginLogMapper;



    /**
     *
     * @author yushuo
     * @description //登录日志列表
     * @date 16:38 2021/2/1
     * @param [dto, roleName, roleCode]
     * @return com.easyboot.framework.domain.DataTableDto
     **/
    public DataTableDto getPageList(DataTableDto dto,String loginName,int loginStatus, String ip, Date startDate,Date endDate){
        endDate= DateUtil.offsetDay(endDate,1);
        LambdaQueryWrapper<SysLoginLog> query=new LambdaQueryWrapper<>();
        query.ge(SysLoginLog::getRequestTime,startDate)
                .le(SysLoginLog::getRequestTime,endDate);
        if(loginStatus>=0){
            query.eq(SysLoginLog::getLoginStatus,loginStatus);
        }
        if(StrUtil.isNotEmpty(ip)){
            query.eq(SysLoginLog::getRequestIp,ip);
        }
        if(StrUtil.isNotEmpty(loginName)){
            query.eq(SysLoginLog::getLoginName,loginName);
        }
        query.orderByDesc(SysLoginLog::getRequestTime) ;
        Page<SysLoginLog> page = new Page<>(dto.getPageNumber(), dto.getPageSize());
        IPage<SysLoginLog> userIPage = loginLogMapper.selectPage(page, query);
        dto.setTotal(userIPage.getTotal());
        dto.setRows(userIPage.getRecords());
        return dto;
    }


    /**
     * @author yushuo
     * @description 保存登录日志
     * @date 16:08 2021/6/25
     * @param
     * @return
    **/
    public  void addLoginLog(SysLoginLog log){
        loginLogMapper.insert(log);
    }


    /**
    *
     * @author yushuo
     * @description 删除登录日志
     * @date 16:42 2021/6/25
     * @param [ids]
     * @return void
    **/
    public void removeAll(String ids){
        List<Integer> idsArray= CommonUtil.toIntList(ids);
        loginLogMapper.deleteBatchIds(idsArray);
    }

    /**
     *
     * @author yushuo
     * @description 清空登录日志
     * @date 16:42 2021/6/25
     * @param [ids]
     * @return void
     **/
    public void clearAll(){
          loginLogMapper.clearLoginLog();
    }
}




